package com.lujianfei.filesend.base

import android.content.Context
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner

abstract class BaseView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs), ViewModelStoreOwner {

    //定义ViewModelStore变量
    private var mViewModelStore: ViewModelStore = ViewModelStore()

    inline fun <reified VM: ViewModel> buildViewModel() = ViewModelProvider(this)[VM::class.java]

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mViewModelStore.clear()
    }

    override fun getViewModelStore(): ViewModelStore {
        return mViewModelStore
    }
}